const {ethers} = require("hardhat");
const copyAbi = require('./copyAbi');

async function main() {
    // 使用 Hardhat 的第一个默认账户
    const [deployer] = await ethers.getSigners();
    
    console.log("开始部署合约...");
    console.log("部署账户:", deployer.address);
    
    // 1. 获取合约工厂
    const PandasNFTFactory = await ethers.getContractFactory("PandasNFT");
    const TreasureTokenFactory = await ethers.getContractFactory("TreasureToken");
    const CollectionNFTFactory = await ethers.getContractFactory("PandasCollection");
    const NFTMarketFactory = await ethers.getContractFactory("PandasMarket");
    
    // 获取当前 nonce
    const nonce = await deployer.getTransactionCount();
    
    // 2. 按顺序部署合约，使用固定的 nonce
    // 首先部署代币合约
    const treasureToken = await TreasureTokenFactory.deploy(deployer.address, {
        nonce: nonce
    });
    await treasureToken.deployed();
    console.log(`TreasureToken合约已部署到: ${treasureToken.address}`);
    
    // 部署收藏系统合约
    const collectionSystem = await CollectionNFTFactory.deploy(deployer.address, treasureToken.address, {
        nonce: nonce + 1
    });
    await collectionSystem.deployed();
    console.log(`PandasCollection合约已部署到: ${collectionSystem.address}`);
    
    // 然后部署NFT合约
    const pandasNFT = await PandasNFTFactory.deploy(deployer.address, treasureToken.address, collectionSystem.address, {
        nonce: nonce + 2
    });
    await pandasNFT.deployed();
    console.log(`PandasNFT合约已部署到: ${pandasNFT.address}`);
    
    // 部署市场合约
    const nftMarket = await NFTMarketFactory.deploy(deployer.address, pandasNFT.address, treasureToken.address, {
        nonce: nonce + 3
    });
    await nftMarket.deployed();
    console.log(`NFTMarket合约已部署到: ${nftMarket.address}`);

    // 设置PandasNFT中的系统合约地址
    console.log("设置PandasCollection中的系统合约地址...");
    await collectionSystem.setNFTContract(pandasNFT.address);
    console.log("系统合约地址设置完成");

    // 初始化设置
    // 1. 为各系统合约分配初始代币
    console.log("为系统合约分配初始代币...");
    await treasureToken.rewardPlayer(collectionSystem.address, ethers.utils.parseEther("1000"));
    await treasureToken.rewardPlayer(nftMarket.address, ethers.utils.parseEther("1000"));
    await treasureToken.rewardPlayer(pandasNFT.address, ethers.utils.parseEther("1000"));
    
    const collectionBalance = await treasureToken.balanceOf(collectionSystem.address);
    const pandasNFTBalance = await treasureToken.balanceOf(pandasNFT.address);
    const marketBalance = await treasureToken.balanceOf(nftMarket.address);
    
    console.log(`收藏系统合约初始代币数量: ${ethers.utils.formatEther(collectionBalance)}`);
    console.log(`NFT合约初始代币数量: ${ethers.utils.formatEther(pandasNFTBalance)}`);
    console.log(`市场合约初始代币数量: ${ethers.utils.formatEther(marketBalance)}`);
    
    console.log("合约部署和初始化完成!");
    console.log("开始复制合约 ABI...");
    copyAbi('PandasNFT', pandasNFT.address);
    copyAbi('TreasureToken', treasureToken.address);
    copyAbi('PandasCollection', collectionSystem.address);
    copyAbi('PandasMarket', nftMarket.address);
    console.log("ABI 复制完成");
    
    // 输出所有合约地址，方便后续使用
    console.log("合约地址汇总:");
    console.log({
        treasureToken: treasureToken.address,
        pandasNFT: pandasNFT.address,
        PandasCollection: collectionSystem.address,
        nftMarket: nftMarket.address
    });
}

// 运行部署脚本
main()
    .then(() => process.exit(0))
    .catch((error) => {
        console.error(error);
        process.exit(1);
    });

// 0x7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6
//0x8b3a350cf5c34c9194ca85829a2df0ec3153be0318b5e2d3348e872092edffba
//0x92db14e403b83dfe3df233f83dfa3a0d7096f21ca9b0d6d6b8d88b2b4ec1564e